package file;

import java.io.File;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ASUS
 * Date: 2025-02-28
 * Time: 10:09
 */
public class Demo10 {
    private static  Scanner scanner = new Scanner(System.in);
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要搜索的根目录: ");
        File rootPath = new File(scanner.next()) ;
        System.out.println("请输入要删除的关键词:");
        String word = scanner.next();

        if (!rootPath.isDirectory()) {
            System.out.println("您此时输入的路径不是合法目录!");
            return;
        }

        scanDir(rootPath, word);
    }

    public static void scanDir(File currentDir, String word) {
        File[] files = currentDir.listFiles();
        if (files == null || files.length == 0) {
            return;
        }

        for (File f: files) {
            System.out.println(f.getAbsolutePath());
            if (f.isFile()) {
                dealFile(f, word);
            }else {
                scanDir(f, word);
            }
        }
    }

    private static void dealFile(File f, String word) {
        if (!f.getName().contains(word)) {
            System.out.println("未找到你所输入的关键字文件, 程序将结束!");
            return;
        }
        System.out.println("该文件是: "+ f.getAbsolutePath()  + ", 是否要确认删除? (Y/N)");
        String choise = scanner.next();
        if (choise.equals("Y") || choise.equals("y")) {
            f.delete();
        }

    }
}
